/**
 * @author JeffYoung
 * @create 2017年3月23日
 */
package com.scau.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.scau.dao.BaseDao;
import com.scau.dao.IStoreInfoDao;
import com.scau.vo.ServiceCategory;
import com.scau.vo.StoreInfo;

public class StoreInfoDaoImpl extends BaseDao implements IStoreInfoDao{

	private Session session = null;
	private Transaction tx = null;
	private String hql = "";
	
	/* (non-Javadoc)
	 * @see com.scau.dao.IStoreInfoDao#addStoreInfo(com.scau.vo.StoreInfo)
	 */
	@Override
	public boolean saveStoreInfo(StoreInfo storeInfo) {
		session = getSession();
		tx = session.beginTransaction();
		session.save(storeInfo);
		tx.commit();
		session.close();
		return true;
	}

	/* (non-Javadoc)
	 * @see com.scau.dao.IStoreInfoDao#delStoreInfo(int)
	 */
	@Override
	public boolean deleteStoreInfo(int id) {
		try {
			session = getSession();
			tx = session.beginTransaction();
			StoreInfo si = (StoreInfo) session.get(
					StoreInfo.class, id);
			session.delete(si);
			tx.commit();
			session.close();	
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see com.scau.dao.IStoreInfoDao#showStoreInfo()
	 */
	@Override
	public List<StoreInfo> listStoreInfo() {
		session = getSession();
		hql = "from StoreInfo si";
		Query query = session.createQuery(hql);
		List<StoreInfo> siList = query.list();
		
		session.close();
		return siList;
	}

	/* (non-Javadoc)
	 * @see com.scau.dao.IStoreInfoDao#updateInfo(com.scau.vo.StoreInfo)
	 */
	@Override
	public boolean updateStoreInfo(StoreInfo storeInfo) {
		session = getSession();
		tx = session.beginTransaction();
		// 根据id获取要更新的实体
		hql = "from StoreInfo si";
		Query query = session.createQuery(hql);
		List<StoreInfo> siList = query.list();
		int id = siList.get(0).getId();
		StoreInfo si = (StoreInfo) session.get(StoreInfo.class, id);
		if (si != null) {
			si.setAddress(storeInfo.getAddress());
			si.setStoreInfo(storeInfo.getStoreInfo());
			si.setName(storeInfo.getName());
			si.setPhone(storeInfo.getPhone());
			si.setGmtModified(storeInfo.getGmtModified());
			//session.clear();
			//session.update(storeInfo);
			tx.commit(); 
			session.close();
			return true;
		}else {
			tx.commit();
			session.close();
			return false;
		}
	}

}
